//
//  LRCCMenu.m
//  lineRunner_cocos2d
//
//  Created by chen jingsen on 13-1-17.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import "LRCCMenu.h"


@implementation LRCCMenu

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	if( state_ != kCCMenuStateWaiting || !visible_ || ! enabled_)
		return NO;
    
	for( CCNode *c = self.parent; c != nil; c = c.parent )
		if( c.visible == NO )
			return NO;
    
	selectedItem_ = [self itemForTouch:touch];
	[selectedItem_ selected];
    
	if( selectedItem_ ) {
		state_ = kCCMenuStateTrackingTouch;
		return YES;
	}
	return NO;
}

-(CCMenuItem *) itemForTouch: (UITouch *) touch
{
	CGPoint touchLocation = [touch locationInView: [touch view]];
	touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    
	CCMenuItem* item;
	CCARRAY_FOREACH(children_, item){
		// ignore invisible and disabled items: issue #779, #866
		if ( [item visible] && [item isEnabled] ) {
            
			CGPoint local = [item convertToNodeSpace:touchLocation];
			CGRect r = [item rect];
			r.origin = CGPointZero;
            
			if( CGRectContainsPoint( r, local ) )
				return item;
		}
	}
	return nil;
}

+(id) menuWithItem:(CCMenuItem*)item
{
    return [CCMenu menuWithItems:item, nil]; //menuWithItems(item, NULL);
}

+(id) menuWithItems: (CCMenuItem*) item, ...
{
	va_list args;
	va_start(args,item);
    
	id s = [[[self alloc] initWithItems: item vaList:args] autorelease];
    
	va_end(args);
	return s;
}

@end
